<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller 
{
     public function registerform(){
	   	$this->regist = U('register');
	   	$this->verification =U('verification');
	   	$this->display();
   }

   public function verification(){
		   	$config =    array(
		    'fontSize'    =>    20,    // 验证码字体大小
		    'length'      =>    4,     // 验证码位数
		    'useNoise'    =>    false, // 关闭验证码杂点
		    'fontttf'     =>    '5.ttf',
		    'imageW'      =>     205,
		    'imageH'      =>     50,
		    'useImgBg'    =>     true,
			);
			$Verify =     new \Think\Verify($config);
			$Verify->entry();
   }

   public function register(){ 
	   	 $CustomerModel = D('customer');
	   	 $verify = new \Think\Verify();
	     $verification = $verify->check(I('post.verification'));
	   	 if (!$verification) {
	   	 	exit($this->error('验证码不正确',U('registerform')));
	   	 }
   	     $pass = I('post.pwd',0);
         $pass1 = I('post.pwd1',0);
         if($pass!=$pass1){
             echo $this->error('两次输入的密码不一致',U('registerform'));
             exit;
         }
         // $user = I('post.user');
	     $data = array(
	    			'user' => I('post.user'),
	                'pwd' => md5(I('post.pwd')),
	                'verification' => I('post.verification'),
	                // 'pid' => I('post.pid',0)               
	    			);
	         
        if (!$CustomerModel->create($data)){
		     // 如果创建失败 表示验证没有通过 输出错误提示信息
		     exit($CustomerModel->getError());
		}else{
		     // 验证通过 可以进行其他数据操作
		      if(!$CustomerModel->add()){
                exit($CustomerModel->getError());
			  }else{
			  	redirect('/shop/home/userright/loginform.html', 2, '页面跳转中...');
			  }
		}
   }

      public function loginform(){
	   	$this->login = U('login');
	   	$this->display();
   }

     public function login(){
	      $user=I('post.user','');
	      $pwd=md5(I('post.pwd',0));
	      $loginModel = M('customer');
	      $row = $loginModel->where("user='".$user."'")->find();
	      if (!empty($row)) {
	      	 if ($pwd==$row['pwd']) {
	      	 	session('user_id',$row['id']);
	      	 	session('username',$row['user']);
	      	 	$this->success('成功登陆',U('home/index/index'));
	      	 }else{
	      	 	$this->error('密码不一致',U('loginform'));
	      	 }
	      }else{
	            $this->error('用户不存在',U('loginform'));
	           }
   }



}	
